package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.UserWhxfViewBinding;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/9 14:56
 * description:
 */
public class UserWhxfAdapter extends DelegateAdapter.Adapter<UserWhxfAdapter.UserWhxfHolder> {

    private Context mContext;
    private LayoutHelper mGridLayoutHelper;
    private ArrayList<View> mView = new ArrayList<>();
    private LinearLayout userAddCard;

    public UserWhxfAdapter(Context context, DefaultLayoutHelper whxfHelp) {
        mContext = context;
        mGridLayoutHelper = whxfHelp;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mGridLayoutHelper;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.user_whxf_view;
    }

    @Override
    public UserWhxfHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return UserWhxfHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(UserWhxfHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(int position, UserWhxfViewBinding binding) {
        ViewPager userCardVp = binding.userCardVp;
        userAddCard = binding.userAddCard;
        userAddCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JumpUtils.jump(mContext,"useraddcard",0);
            }
        });

        List<View> viewList = new ArrayList<View>();

//        LayoutInflater layoutInflater = mContext.getLayoutInflater();
        View view = LayoutInflater.from(mContext).inflate(R.layout.user_vp_view, null, false);
//        View view1 = LayoutInflater.from(mContext).inflate(R.layout.user_vp_view, null, false);
//        View view2 = LayoutInflater.from(mContext).inflate(R.layout.user_vp_view, null, false);
//        View view3 = LayoutInflater.from(mContext).inflate(R.layout.user_vp_view, null, false);

        viewList.add(view);
//        viewList.add(view);
//        viewList.add(view2);
//        viewList.add(view3);

        MyPagerAdapter vpAdapter = new MyPagerAdapter(viewList);

        userCardVp.setAdapter(vpAdapter);
        userCardVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    @Override
    public int getItemCount() {
        return 1;
    }

    static class UserWhxfHolder extends RecyclerView.ViewHolder {

        private UserWhxfViewBinding binding;

        public static UserWhxfHolder creat(ViewGroup parent) {
            UserWhxfViewBinding binding = UserWhxfViewBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new UserWhxfHolder(binding);
        }

        public UserWhxfHolder(UserWhxfViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    private class MyPagerAdapter extends PagerAdapter {

        private List<View> mViewList;

        MyPagerAdapter(List<View> viewList) {
            mViewList = viewList;
        }

        @Override
        public int getCount() {
            return mViewList.size();
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mViewList.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewList.get(position));
        }

    }

}
